package net.wanho.service.impl;

import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import net.wanho.service.CaptchaService;
import net.wanho.vo.CaptchaVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * Author：汤小洋
 * Date：2025-01-12 15:31
 * Description：<描述>
 */
@Service
public class CaptchaServiceImpl implements CaptchaService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public CaptchaVo captcha() {
        Captcha captcha = new SpecCaptcha();

        String uuid = UUID.randomUUID().toString();
        String code = captcha.text();
        redisTemplate.opsForValue().set("captcha:" + uuid, code, 5, TimeUnit.MINUTES);

        CaptchaVo captchaVo = new CaptchaVo();
        captchaVo.setUuid(uuid);
        captchaVo.setImg(captcha.toBase64());

        return captchaVo;
    }
}
